﻿using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using Jin.Core.Schema;
using Jin.Core.Linq.Structure;
using Jin.Core.Query;
using System.Data.Common;
using System.Data;
using System.Reflection;
namespace Jin.Core.DataProviders
{
    public interface IDataProvider
    {
        /// <summary>
        /// 数据驱动名称
        /// </summary>
        string DbDataProvierName { get; }
        /// <summary>
        /// 名称
        /// </summary>
        string Name { get; }
        [Obsolete("Use SetLogger method instead")]
        TextWriter Log { get; set; }

        //void SetLogger(ILogAdapter logger);
       // void SetLogger(Action<String> logger);
        /// <summary>
        /// 持有的名单表、视图、存储过程等。
        /// </summary>
        IDatabaseSchema Schema { get; }
        ISchemaGenerator SchemaGeneretor { get; }
        ISqlfragment Sqlfragment { get; }
        IQueryLanguage QueryLanguage { get; }
        ISqlGenerator GetSqlGenerator(SqlQuery query);

        /// <summary>
        /// 参数前缀
        /// </summary>
        string ParameterPrefix { get; }
        /// <summary>
        /// 数据库连接
        /// </summary>
        DbConnection CurrentSharedConnection { get; }
        /// <summary>
        /// 数据库连接字符串
        /// </summary>
        string ConnectionString { get; }
        /// <summary>
        /// 数据库工厂
        /// </summary>
        DbProviderFactory Factory { get; }
        /// <summary>
        /// 获取表从数据库
        /// </summary>
        /// <param name="tabelName">表名</param>
        /// <returns></returns>
        ITable GetTableFromDB(string tabelName);

        /// <summary>
        /// 执行读取行
        /// </summary>
        /// <param name="cmd">查询命令类型</param>
        /// <returns></returns>
        DbDataReader ExecuteReader(QueryCommand cmd);
        /// <summary>
        /// 执行读取内存表
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        DataSet ExecuteDataSet(QueryCommand cmd);

        IList<T> ToList<T>(QueryCommand cmd) where T : new();
        IEnumerable<T> ToEnumerable<T>(QueryCommand<T> cmd, object[] paramValues);
        /// <summary>
        /// 执行ExecuteScalar
        /// </summary>
        /// <param name="cmd"></param>
        /// <returns></returns>
        object ExecuteScalar(QueryCommand cmd);
        /// <summary>
        /// 返回一个指定类型
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="cmd"></param>
        /// <returns></returns>
        T ExecuteSingle<T>(QueryCommand cmd) where T : new();
        int ExecuteQuery(QueryCommand cmd);
        ITable FindTable(string tableName);
        ITable FindOrCreateTable<T>() where T : new();
        ITable FindOrCreateTable(Type type);
        DbCommand CreateCommand();
        #region SQL格式部分
        /// <summary>
        /// 合法的表名
        /// </summary>
        /// <param name="tbl"></param>
        /// <returns></returns>
        string QualifyTableName(ITable tbl);
        /// <summary>
        /// 合法的列名
        /// </summary>
        /// <param name="column"></param>
        /// <returns></returns>
        string QualifyColumnName(IColumn column);
        /// <summary>
        /// 合法的存储过程
        /// </summary>
        /// <param name="sp"></param>
        /// <returns></returns>
        string QualifySPName(IStoredProcedure sp);
        /// <summary>
        /// 插入身份获取字符串
        /// </summary>
        string InsertionIdentityFetchString { get; }
        #endregion
        #region 连接部分
        DbConnection InitializeSharedConnection(string connectionString);
        DbConnection InitializeSharedConnection();
        void ResetSharedConnection();
        DbConnection CreateConnection();
        void MigrateToDatabase<T>(Assembly assembly);
        void MigrateNamespaceToDatabase(string modelNamespace, Assembly assembly);
        #endregion
    }
}
